package com.sojson.util.serialize;

import java.io.IOException;

import com.sojson.util.serialize.impl.SerializeUtilImpl;

/**
 * 序列化工具类
 * 
 * @author liu
 * @date 2020-11-02
 */
public class SerializeUtil {

    private static final SerializeUtilInterface SERIALIZE_UTIL_INTERFACE = SerializeUtilImpl.getInstances();

    /**
     * 将对象转换为字节数组
     * 
     * @param value 要转换的对象
     * @return 转换后的数组
     * @throws IOException
     */
    public static byte[] serialize(Object value) throws IOException {
        return SERIALIZE_UTIL_INTERFACE.serialize(value);
    }

    /**
     * 将字节数组转换为Object类型的对象
     * 
     * @param in 要转换的字节数组
     * @return 转换后的对象
     * @throws IOException
     * @throws ClassNotFoundException
     */
    public static Object deserialize(byte[] in) throws ClassNotFoundException, IOException {
        return SERIALIZE_UTIL_INTERFACE.serialize(in);
    }

    /**
     * 将字节数组转换为指定类型的对象
     * 
     * @param in 要转换的字节数组
     * @param requiredType 转换后的对象的类型
     * @return 转换后的对象
     * @throws IOException
     * @throws ClassNotFoundException
     */
    public static <T> T deserialize(byte[] in, Class<T> requiredType) throws IOException, ClassNotFoundException {
        return SERIALIZE_UTIL_INTERFACE.deserialize(in, requiredType);
    }

}